package edzest.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Font;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;

import edzest.dao.MemberDAO;
import edzest.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class AddMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField uid;
	private JPasswordField pwd;
	private JPasswordField repwd;
	private MemberDAO mdao = new MemberDAO();
		// static을 이용하면 new 연산자 없이
		// 객체를 바로 사용할 수 있음
		// 하지만, static 없이 클래스 수준의 멤버변수로 선언해서
		// 모든 메서드가 다 같이 쓸수 있게 할 수 있음

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AddMember dialog = new AddMember();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddMember() {
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		
		name = new JTextField();
		name.setColumns(10);
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		uid = new JTextField();
		uid.setColumns(10);
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		JLabel txtRepwd = new JLabel("비밀번호확인");
		txtRepwd.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("맑은 고딕", Font.BOLD, 16));
		
		JButton checkUidBtn = new JButton("중복");
		checkUidBtn.setFont(new Font("굴림", Font.BOLD, 12));
		
		pwd = new JPasswordField();
		pwd.setColumns(16);
		
		repwd = new JPasswordField();
		repwd.setColumns(16);
		
		JRadioButton isMale = new JRadioButton("남자");
		isMale.setSelected(true);
		isMale.setFont(new Font("굴림", Font.BOLD, 12));
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setSelected(true);
		isFemale.setFont(new Font("굴림", Font.BOLD, 12));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(154)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtRepwd)
								.addComponent(txtName))
							.addGap(50)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(name, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(isFemale, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE))
								.addComponent(repwd, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
									.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
										.addComponent(uid, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(checkUidBtn, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
									.addComponent(pwd, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
					.addContainerGap(119, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(74)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(txtName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(3)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(checkUidBtn))))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtRepwd, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
								.addComponent(isMale)
								.addComponent(isFemale)))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(167, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton joinBtn = new JButton("가입완료");
				joinBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					//	if ( ! pwd.getPassword().equals(repwd.getPassword() ) )
					//	JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않아요!");
					//	getPassword()가 char[]을 반환하므로 pwd랑 repwd 비교해도 계속 다르다고 나옴
						if ( ! Arrays.equals( pwd.getPassword(), repwd.getPassword() ) )
							JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않아요!");
						else {
							
							MemberVO m = new MemberVO(null, uid.getText(), new String( pwd.getPassword() ), name.getText(), "", null);
							if ( mdao.insertMember(m) > 0 )
								JOptionPane.showMessageDialog(null, "회원가입완료!");
						}
					}
				});
				joinBtn.setActionCommand("OK");
				buttonPane.add(joinBtn);
				getRootPane().setDefaultButton(joinBtn);
			}
		}
	}
}
